package com.xinfu.newproject.adapter.preperty_adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.xinfu.newproject.R;
import com.xinfu.newproject.bean.TestPropertyPayCostBean;

import java.util.List;

public class PropertyAdapter extends RecyclerView.Adapter<PropertyAdapter.RecyclerViewHolder> {

    private List<TestPropertyPayCostBean> testPropertyPayCostBeans;

    //上下文和数据
    Context context;

    //构造方法
    public PropertyAdapter(Context context, List<TestPropertyPayCostBean> testPropertyPayCostBeans) {
        this.context = context;
        this.testPropertyPayCostBeans = testPropertyPayCostBeans;
    }

    //添加数据
    public void addbean(List<TestPropertyPayCostBean> list) {
        this.testPropertyPayCostBeans = list;
        Log.i("testOne", testPropertyPayCostBeans.get(1).isClickble() + "list3");
        //更新数据
        notifyDataSetChanged();
    }


    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //创建布局，并保存在ViewHolder中
        return new RecyclerViewHolder(LayoutInflater.from(context).inflate(R.layout.item_item_property_one, parent, false));
    }


    @Override
    public void onBindViewHolder(final RecyclerViewHolder holder, final int position) {
        //设置具体的数据
        if (testPropertyPayCostBeans != null) {
            holder.priceTv.setText(testPropertyPayCostBeans.get(position).getPrice());
            holder.timeTv.setText(testPropertyPayCostBeans.get(position).getStringTime());
//        if (testPropertyPayCostBeans.get(position).isClickble()) {
//            holder.checkBox.setChecked(true);
//        } else {
            holder.checkBox.setChecked(testPropertyPayCostBeans.get(position).isClickble());
            Log.i("testOne", testPropertyPayCostBeans.get(1).isClickble() + "list2");
            holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    testPropertyPayCostBeans.get(position).setClickble(b);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return testPropertyPayCostBeans == null ? 0 : testPropertyPayCostBeans.size();
    }



    //横向的RecyclerView
    public class RecyclerViewHolder extends RecyclerView.ViewHolder {

        public CheckBox checkBox;
        public TextView timeTv;
        public TextView priceTv;

        public RecyclerViewHolder(View itemView) {
            super(itemView);
            timeTv = itemView.findViewById(R.id.item_item_property_one_time_tv);
            priceTv = itemView.findViewById(R.id.item_item_property_one_price_tv);
            checkBox = itemView.findViewById(R.id.item_item_property_one_cb);
        }
    }

}

